package com.valveinspectionsys.routing.BaseAdapter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.valveinspectionsys.R;
import com.valveinspectionsys.povo.ListViewInfo;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;



public class ListViewAdapter extends BaseAdapter {

    private static Map<Integer, View> m = new HashMap<Integer, View>();

    private List<ListViewInfo> items;
    private LayoutInflater inflater;

    public ListViewAdapter(List<ListViewInfo> items, Context context) {
	super();
	this.items = items;
	this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {

	return items.size();
    }

    @Override
    public Object getItem(int position) {

	return items.get(position);
    }

    @Override
    public long getItemId(int position) {

	return position;
    }

    @Override
    public View getView(int position, View contentView, ViewGroup arg2) {

	contentView = m.get(position);
	if (contentView == null) {
	    contentView = inflater.inflate(R.layout.listview_item, null);
	    TextView text = (TextView) contentView.findViewById(R.id.list_item_text);
	    TextView text1 = (TextView) contentView.findViewById(R.id.listview_item_username);
	    TextView text2 = (TextView) contentView.findViewById(R.id.listview_item_wateradds);
	    text.setText(items.get(position).getNumber());
	    text1.setText(items.get(position).getUserinfo());
	    text2.setText(items.get(position).getWaterinfo());
	    if (items.get(position).getColor() > 0) {
		text.setTextColor(Color.GRAY);
		text1.setTextColor(Color.GRAY);
		text2.setTextColor(Color.GRAY);
	    }
	}
	m.put(position, contentView);
	return contentView;
    }

    /**
     * 删除所有数据
     */
    public void removeItem() {
	items.clear();
	m.clear();
    }

    /**
     * 添加数据
     * 
     * @param item
     */
    public void addItem(ListViewInfo item) {

	items.add(item);
    }

}
